<?php

namespace app\admin\controller\business;

use app\common\controller\Backend;

class Highsea extends Backend
{
    // 设置关联查询
    protected $relationSearch = true;

    // 当前模型
    protected $model = null;

    // 无需登陆
    protected $noNeedLogin = [];
    // 无需鉴权
    protected $noNeedRight = [];

    public function __construct(){
        parent::__construct();

        $this->model = model('Business.User');
        $this->SourceModel = model('Business.Source');
        $this->AdminModel = model('Admin');
        $this->ReceiveModel = model('Business.Receive');
    }

    public function index(){
        // 接收请求，查询数据
        // 判断是否有ajax请求

        //将请求当中所有的参数去除html标签，去掉两边空白
        $this->request->filter(['strip_tags', 'trim']);

        if($this->request->isAjax()){
            //获取表格所提交过来筛选和排序的一些参数
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            // 表格需要两个返回值参数 总条数和分页的数据
            $total = $this->model
                ->with(['source'])
                ->where($where)
                ->where('adminid',NULL)
                ->order($sort,$order)
                ->count(); //返回查询的总数

            // var_dump($total);
            // exit;
            $list = $this->model
                ->with(['source'])
                ->where($where)
                ->where('adminid',NULL)
                ->order($sort,$order)
                ->limit($offset,$limit)
                ->select();
            // var_dump(collection($list)->toArray());
            // exit;

            // 打印sql语句
            // echo $this->model->getLastSql();
            // exit;

            // 组装数据
            $result = [
                'total' => $total,
                'rows' => $list
            ];
            // 返回给ajax的数据
            return json($result);  

        }
        return $this->view->fetch();
    }

    public function del($ids=NULL){
        $list = $this->model->select($ids);
        // var_dump($list);
        // exit;

        // 没有数据
        if(!$list){
            $this->error(__('No Results were found'));
            exit;
        }

        // 软删除
        $result = $this->model->destroy($ids);
        
        if($result === FALSE){
            $this->error("删除失败");
            exit;
        }else{
            $this->success("删除成功");
            exit;
        }
    }

    public function apply($ids=NULL){
        $ids = !empty($ids) ? explode(",",$ids) : [];
        $row = $this->model->all($ids);

        if(!$row){
            $this->error(__('No Results were found'));
            exit;
        }
        // var_dump($row);
        // exit;
        // $applyid = $this->auth->id;
        // var_dump($applyid);
        // exit;

        $list = []; // 领取表
        $businesslist = []; // 客户表

        foreach($row as $value){
            $list[] = [
                'applyid' => $this->auth->id,
                'status' => 'apply',
                'busid' => $value['id']
            ];

            $businesslist[] = [
                'id' => $value['id'],
                'adminid' => $this->auth->id
            ];
        }

        // var_dump($list);
        // var_dump($businesslist);
        // exit;

        $this->model->startTrans();
        $this->ReceiveModel->startTrans();

        // 更新用户表
        $BusinessStatus = $this->model->saveAll($businesslist);
        if($BusinessStatus === FALSE){
            $this->error($this->model->getError());
            exit;
        }

        // 更新领取表
        $ReceiveStatus = $this->ReceiveModel->saveAll($list);
        if($ReceiveStatus === FALSE){
            $this->model->rollback();
            $this->error($this->ReceiveModel->getError());
        }

        if($ReceiveStatus === FALSE || $BusinessStatus === FALSE){
            $this->model->rollback();
            $this->ReceiveModel->rollback();
            $this->error('申请失败');
        }else{
            $this->model->commit();
            $this->ReceiveModel->commit();
            $this->success('申请成功');
        }

        
    }

    public function allocation($ids = NULL){
        $ids = !empty($ids) ? explode(",",$ids) : [];
        $row = $this->model->all($ids);
        // var_dump($row);
        // exit;


        if(!$row){
            $this->error(__('No Results were found'));
            exit;
        }

        if($this->request->isPost()){
            $param = $this->request->post("row/a");
            // var_dump($param);
            // exit;
            $list = [];
            $businesslist = [];

            foreach($row as $value){
                $list[] = [
                    'applyid' => $param['adminid'],
                    'status' => 'allot',
                    'busid' => $value['id']
                ];
    
                $businesslist[] = [
                    'id' => $value['id'],
                    'adminid' => $param['adminid']
                ];
            }

            // 开启事务
            $this->model->startTrans();
            $this->ReceiveModel->startTrans();

            // 更新用户表
            $BusinessStatus = $this->model->saveAll($businesslist);

            if($BusinessStatus === FALSE){
                $this->error($this->model->getError());
                exit;
            }

            // 更新申请表
            $ReceiveStatus = $this->ReceiveModel->saveAll($list);
            
            if($ReceiveStatus === FALSE){
                $this->model->rollback();
                $this->error($this->model->getError());
                exit;
            }

            if($BusinessStatus === FALSE && $ReceiveStatus === FALSE){
                $this->model->rollback();
                $this->ReceiveModel->rollback();
                $this->error('分配失败');
            }else{
                $this->model->commit();
                $this->ReceiveModel->commit();
                $this->success('分配成功');
            }

            

        }


        $AdminData = $this->AdminModel->column('id,username');
        // var_dump($AdminData);
        // exit;
        $this->assign([
            'AdminData' => $AdminData,
            'row' => $row
        ]);

        return $this->view->fetch();
    }

}
